home *** CD-ROM | disk | FTP | other *** search
/ Experimental BBS Explossion 3 / Experimental BBS Explossion III.iso / games / nhak_src.zip / UNIXTTY.C < prev    next >
C/C++ Source or Header  |  1993-03-16  |  5KB  |  205 lines

  1. /*    SCCS Id: @(#)unixtty.c    3.0    88/05/03
  2. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
  3. /* NetHack may be freely redistributed.  See license for details. */
  4. /* tty.c - (Unix) version */
  5.  
  6. /* With thanks to the people who sent code for SYSV - hpscdi!jon,
  7.  * arnold@ucsf-cgl, wcs@bo95b, cbcephus!pds and others.
  8.  */
  9.  
  10. /* block some unused #defines to avoid overloading some cpp's */
  11. #define MONATTK_H
  12. #define ONAMES_H
  13. #define NEED_VARARGS
  14. #include "hack.h"
  15.  
  16. /*
  17.  * The distinctions here are not BSD - rest but rather USG - rest, as
  18.  * BSD still has the old sgttyb structure, but SYSV has termio. Thus:
  19.  */
  20. #if defined(BSD) || defined(ULTRIX)
  21. #define    V7
  22. #else
  23. #define USG
  24. #endif
  25.  
  26.  
  27. #ifdef USG
  28.  
  29. #include    <termio.h>
  30. #define termstruct    termio
  31. #define kill_sym    c_cc[VKILL]
  32. #define erase_sym    c_cc[VERASE]
  33. #define intr_sym    c_cc[VINTR]
  34. #define EXTABS        TAB3
  35. #define tabflgs        c_oflag
  36. #define echoflgs    c_lflag
  37. #define cbrkflgs    c_lflag
  38. #define CBRKMASK    ICANON
  39. #define CBRKON        ! /* reverse condition */
  40. #define OSPEED(x)    ((x).c_cflag & CBAUD)
  41. #define IS_7BIT(x)    ((x).c_cflag & CS7)
  42. #define inputflags    c_iflag
  43. #define STRIPHI        ISTRIP
  44. #define GTTY(x)        (ioctl(0, TCGETA, x))
  45. /* STTY now modified to run under Sys V R3.    - may have to be #ifdef'ed */
  46. #define STTY(x)        (ioctl(0, TCSETAW, x))    /* TCSETAF? TCSETAW? */
  47. #define GTTY2(x)    1
  48. #define STTY2(x)    1
  49. #define nonesuch    0
  50. #define inittyb2    inittyb
  51. #define curttyb2    curttyb
  52.  
  53. #else    /* V7 */
  54.  
  55. #include    <sgtty.h>
  56. #define termstruct    sgttyb
  57. #define    kill_sym    sg_kill
  58. #define    erase_sym    sg_erase
  59. #define    intr_sym    t_intrc
  60. #define EXTABS        XTABS
  61. #define tabflgs        sg_flags
  62. #define echoflgs    sg_flags
  63. #define cbrkflgs    sg_flags
  64. #define CBRKMASK    CBREAK
  65. #define CBRKON        /* empty */
  66. #define inputflags    sg_flags    /* don't know how enabling meta bits */
  67. #define IS_7BIT(x)    (FALSE)
  68. #define STRIPHI        0        /* should actually be done on BSD */
  69. #define OSPEED(x)    (x).sg_ospeed
  70. #define GTTY(x)        (gtty(0, x))
  71. #define STTY(x)        (stty(0, x))
  72. #define GTTY2(x)    (ioctl(0, TIOCGETC, (char *)x))
  73. #define STTY2(x)    (ioctl(0, TIOCSETC, (char *)x))
  74. #define nonesuch    -1
  75. struct tchars inittyb2, curttyb2;
  76.  
  77. #endif
  78.  
  79. extern short ospeed;
  80. char erase_char, intr_char, kill_char;
  81. static boolean settty_needed = FALSE;
  82. struct termstruct inittyb, curttyb;
  83.  
  84. static void
  85. setctty(){
  86.     if(STTY(&curttyb) < 0 || STTY2(&curttyb2) < 0)
  87.         perror("NetHack (setctty)");
  88. }
  89.  
  90. /*
  91.  * Get initial state of terminal, set ospeed (for termcap routines)
  92.  * and switch off tab expansion if necessary.
  93.  * Called by startup() in termcap.c and after returning from ! or ^Z
  94.  */
  95. void
  96. gettty(){
  97.     if(GTTY(&inittyb) < 0 || GTTY2(&inittyb2) < 0)
  98.         perror("NetHack (gettty)");
  99.     curttyb = inittyb;
  100.     curttyb2 = inittyb2;
  101.     ospeed = OSPEED(inittyb);
  102.     erase_char = inittyb.erase_sym;
  103.     kill_char = inittyb.kill_sym;
  104.     intr_char = inittyb2.intr_sym;
  105.     getioctls();
  106.  
  107.     /* do not expand tabs - they might be needed inside a cm sequence */
  108.     if(curttyb.tabflgs & EXTABS) {
  109.         curttyb.tabflgs &= ~EXTABS;
  110.         setctty();
  111.     }
  112.     settty_needed = TRUE;
  113. }
  114.  
  115. /* reset terminal to original state */
  116. void
  117. settty(s)
  118. const char *s;
  119. {
  120.     clear_screen();
  121.     end_screen();
  122.     if(s) Printf(s);
  123.     (void) fflush(stdout);
  124.     if(STTY(&inittyb) < 0 || STTY2(&inittyb2) < 0)
  125.         perror("NetHack (settty)");
  126.     flags.echo = (inittyb.echoflgs & ECHO) ? ON : OFF;
  127.     flags.cbreak = (CBRKON(inittyb.cbrkflgs & CBRKMASK)) ? ON : OFF;
  128.     curttyb.inputflags |= STRIPHI;
  129.     setioctls();
  130. }
  131.  
  132. void
  133. setftty(){
  134. register int ef = 0;            /* desired value of flags & ECHO */
  135. #ifdef LINT    /* cf = CBRKON(CBRKMASK); const expr to initialize is ok */
  136. register int cf = 0;
  137. #else
  138. register int cf = CBRKON(CBRKMASK);    /* desired value of flags & CBREAK */
  139. #endif
  140. register int change = 0;
  141.     flags.cbreak = ON;
  142.     flags.echo = OFF;
  143.     /* Should use (ECHO|CRMOD) here instead of ECHO */
  144.     if((curttyb.echoflgs & ECHO) != ef){
  145.         curttyb.echoflgs &= ~ECHO;
  146. /*        curttyb.echoflgs |= ef;                    */
  147.         change++;
  148.     }
  149.     if((curttyb.cbrkflgs & CBRKMASK) != cf){
  150.         curttyb.cbrkflgs &= ~CBRKMASK;
  151.         curttyb.cbrkflgs |= cf;
  152. #ifdef USG
  153.         /* be satisfied with one character; no timeout */
  154.         curttyb.c_cc[VMIN] = 1;        /* was VEOF */
  155.         curttyb.c_cc[VTIME] = 0;    /* was VEOL */
  156. #endif
  157.         change++;
  158.     }
  159.     if(!IS_7BIT(inittyb)) curttyb.inputflags &=~ STRIPHI;
  160.     /* If an interrupt character is used, it will be overriden and
  161.      * set to ^C.
  162.      */
  163.     if(intr_char != nonesuch && curttyb2.intr_sym != '\003') {
  164.         curttyb2.intr_sym = '\003';
  165.         change++;
  166.     }
  167.  
  168.     if(change) setctty();
  169.     start_screen();
  170. }
  171.  
  172. void
  173. intron() {        /* enable kbd interupts if enabled when game started */
  174.  
  175.     if(intr_char != nonesuch && curttyb2.intr_sym != '\003') {
  176.         curttyb2.intr_sym = '\003';
  177.         setctty();
  178.     }
  179. }
  180.  
  181. void
  182. introff() {        /* disable kbd interrupts if required*/
  183.  
  184.     if(curttyb2.intr_sym != nonesuch) {
  185.         curttyb2.intr_sym = nonesuch;
  186.         setctty();
  187.     }
  188. }
  189.  
  190.  
  191. /* fatal error */
  192. /*VARARGS1*/
  193.  
  194. void
  195. error VA_DECL(const char *,s)
  196.     VA_START(s);
  197.     VA_INIT(s, const char *);
  198.     if(settty_needed)
  199.         settty(NULL);
  200.     Vprintf(s,VA_ARGS);
  201.     (void) putchar('\n');
  202.     VA_END();
  203.     exit(1);
  204. }
  205.